package org.easy.tool.common;

import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * @author Valley
 * Created by Valley on 2023/11/9 15:19
 */
public class PPTUtil {

//    /**
//     * 功能：将ppt转化为pptx
//     */
//    public static void transfer2XSpire(String ppt, String pptx) throws Exception {
//        // 实例化一个表示 PPT 文件的 Presentation 对象
//        Presentation pres = new Presentation();
//        pres.loadFromFile(ppt);
//        // 将 PPT 演示文稿另存为 PPTX
//        pres.saveToFile(pptx, FileFormat.PPTX_2013);
//        pres.dispose();
//    }

    /**
     * 功能：将ppt转化为pptx
     */
    public static void transfer2XPoi(String ppt, String pptx) throws Exception {
        HSLFSlideShow firstPPT = new HSLFSlideShow(Files.newInputStream(Paths.get(ppt)));
        firstPPT.write(Files.newOutputStream(Paths.get(pptx)));
        firstPPT.close();
    }


//    /**
//     * 功能：将指定幻灯片合并到文档
//     * 参数1：所有需要被合并的ppt路径
//     * 参数2：合并到输出的ppt路径
//     */
//    public static void mergePPTSpire(List<String> mergeList, String outPath) throws Exception {
//
//        Presentation ppt = new Presentation();
//        for (String mergeName : mergeList) {
//            Presentation mergePPT = new Presentation();
//            mergePPT.loadFromFile(mergeName);
//            for (int j = 0; j < mergePPT.getSlides().getCount(); j++) {
//                ppt.getSlides().append(mergePPT.getSlides().get(j));
//            }
//        }
//        //保存文档2
//        ppt.saveToFile(outPath, FileFormat.PPTX_2013);
//        ppt.dispose();
//
//
//        //加载第一个PPT文档
//        String firstName = mergeList.get(0);
//        Presentation firstPPT = new Presentation();
//        firstPPT.loadFromFile(firstName);
//
//        //遍历被合并文档的所有幻灯片，添加到第一个文档
//        for (int i = 1; i < mergeList.size(); i++) {
//            String mergeName = mergeList.get(i);
//            Presentation mergePPT = new Presentation();
//            mergePPT.loadFromFile(mergeName);
//            for (int j = 0; j < mergePPT.getSlides().getCount(); j++) {
//                firstPPT.getSlides().append(mergePPT.getSlides().get(j));
//            }
//        }
//
//        //保存文档2
//        firstPPT.saveToFile(outPath, FileFormat.PPTX_2013);
//        firstPPT.dispose();
//
//    }

    /**
     * 功能：将指定幻灯片合并到文档
     * 参数1：所有需要被合并的ppt路径
     * 参数2：合并到输出的ppt路径
     */
    public static void mergePPTPoi(List<String> mergeList, String outPath) throws Exception {

        //加载第一个PPT文档
        String firstName = mergeList.get(0);
        HSLFSlideShow firstPPT = new HSLFSlideShow(Files.newInputStream(Paths.get(firstName)));
        List<HSLFSlide> firstPPTSlides = firstPPT.getSlides();
        //遍历被合并文档的所有幻灯片，添加到第一个文档
        for (int i = 1; i < mergeList.size(); i++) {
            String mergeName = mergeList.get(i);
            HSLFSlideShow mergePPT = new HSLFSlideShow(Files.newInputStream(Paths.get(mergeName)));
            // 遍历每张幻灯片
            List<HSLFSlide> slides = mergePPT.getSlides();
            firstPPTSlides.addAll(slides);
        }

        //保存文档2
        firstPPT.readProperties();
        firstPPT.write(Files.newOutputStream(Paths.get(outPath)));
    }

    /**
     * 功能：将指定幻灯片合并到文档
     * 参数1：所有需要被合并的ppt路径
     * 参数2：合并到输出的ppt路径
     */
    public static void mergePPTXPoi(List<String> mergeList, String outPath) throws Exception {

//        XMLSlideShow ppt = new XMLSlideShow();
//        for (String mergeName : mergeList) {
//            XMLSlideShow mergePPT = new XMLSlideShow(Files.newInputStream(Paths.get(mergeName)));
//            // 遍历每张幻灯片
//            for (XSLFSlide srcSlide : mergePPT.getSlides()) {
//                // 合并
//                ppt.createSlide().importContent(srcSlide);
//            }
//        }
//        ppt.write(Files.newOutputStream(Paths.get(outPath)));
//        ppt.close();

        //加载第一个PPT文档
        String firstName = mergeList.get(0);
        XMLSlideShow firstPPT = new XMLSlideShow(Files.newInputStream(Paths.get(firstName)));
        //遍历被合并文档的所有幻灯片，添加到第一个文档
        for (int i = 1; i < mergeList.size(); i++) {
            String mergeName = mergeList.get(i);
            XMLSlideShow mergePPT = new XMLSlideShow(Files.newInputStream(Paths.get(mergeName)));
            // 遍历每张幻灯片
            for (XSLFSlide srcSlide : mergePPT.getSlides()) {
                // 合并
                firstPPT.createSlide().importContent(srcSlide);
            }
        }

        //保存文档2
        firstPPT.write(Files.newOutputStream(Paths.get(outPath)));
    }

}
